package cn.ekgc.hbase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.BinaryComparator;
import org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.util.Bytes;

public class ScanDemo {
    public static void main(String[] args) throws Exception {
        Configuration config = HBaseConfiguration.create(); //获取链接信息
        //创建表
        //创建链接
        try (Connection connection = ConnectionFactory.createConnection(config)) {
            Admin admin = connection.getAdmin();
            System.out.println("scan...... ");
            //do scan

            Scan scan = new Scan();

            //scan.setRowPrefixFilter("row-".getBytes());
            SingleColumnValueFilter filter = new SingleColumnValueFilter(
                    "personalinfo".getBytes(),
                    "nickname".getBytes(),
                    CompareFilter.CompareOp.EQUAL,
                    new BinaryComparator("张三".getBytes())
            );
            filter.setFilterIfMissing(true);
            scan.setFilter(filter);

            //t.get t.put t.scan t.delete ....
            Table table = connection.getTable(TableName.valueOf("gamer"));
            ResultScanner rs =  table.getScanner(scan); // like jdbc ResultSet
            rs.forEach(result -> {
                String rowkey = Bytes.toString(result.getRow());
                System.out.println("-------------line start---------------"+rowkey);
                result.getMap().forEach((cf,map) ->{
                    String family = Bytes.toString(cf);
                    System.out.println("列族:"+family);
                    map.forEach((qualifier,vmap)->{
                        String column = Bytes.toString(qualifier);
                        System.out.println("列:"+column);
                        vmap.forEach((ts,value)->{
                            System.out.println("时间戳版本:"+ts);
                            System.out.println("值:"+Bytes.toString(value));
                        });
                    });
                });
                System.out.println("-------------line end---------------");
            });


            System.out.println(" Done.");

        }
    }
}
